<?php

namespace app\health\service;

use app\health\model\healthPro\TvService;

/**
 * 平安健康服务
 */
class PajkService
{
    // 渠道
    protected const CHANNEL = 'BJXQY';

    /**
     * 获取服务类型列表
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function serviceList()
    {
        return TvService::where(['type' => ['<>', 0]])->field('product_name as name,iden,type,content')->select();
    }

    /**
     * 获取服务类型
     * @param $type
     * @return array
     */
    public static function getServiceByType($type): array
    {
        $data = TvService::where('type', '>', 0)->column('id,product_name,product_code,iden,type,content', 'type');
        return $data[$type] ?? [];
    }

    /**
     * 获取服务类型
     * @param $iden
     * @return array
     */
    public static function getServiceByIden($iden): array
    {
        $data = TvService::where('iden', '<>', '')->column('id,product_name,product_code,iden,type,content', 'iden');
        return $data[$iden] ?? [];
    }

    /**
     * 获取服务信息
     * @param $type
     * @param $time
     * @param string $name
     * @return array
     */
    public static function infoByType($type, $time, string $name = ''): array
    {
        $row = self::getServiceByType($type);
        return ['name' => $name ?: $row['product_name'], 'time' => $time, 'type' => $type];
    }

    /**
     * 获取服务信息
     * @param $iden
     * @param $time
     * @param string $name
     * @return array
     */
    public static function infoByIden($iden, $time, string $name = ''): array
    {
        $row = self::getServiceByIden($iden);
        return ['name' => $name ?: $row['product_name'], 'content' => $row['content'] ?? '', 'time' => $time, 'type' => $row['type'] ?? 0];
    }

    /**
     * 获取平安健康小程序链接
     * @param $type
     * @param string $ticket 联合登录token
     * @return string
     */
    public static function getPageUrl($type, string $ticket = ''): string
    {
        $row    = self::getServiceByType($type);
        $params = ['ticket' => $ticket, 'redirectCode' => $row['product_code'] ?? '', 'channel' => self::CHANNEL];
        array_filter($params);
        $params = http_build_query($params);
        $page   = $params ? 'pages/bridge/transforlogin/index?' . $params : '';
        return empty($row) ? '' : $page;
    }
}